package com.rocketmq.base.example.producer;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

/**
 * @Auther: tianchao
 * @Date: 2021/9/2 13:28
 * @Description: 同步发送消息
 */

/**
 * 第一个样例 同步发送消息
 */
public class SyncProducer {
    public static void main(String[] args) throws Exception {
        //Instantiate with a producer group name.
        DefaultMQProducer producer = new
                DefaultMQProducer("SyncProducer-group");
        // Specify name server addresses.
        producer.setNamesrvAddr("192.168.1.221:9876");
        //Launch the instance.
        producer.start();
        for (int i = 0; i < 4; i++) {
            //Create a message instance, specifying topic, tag and message body.
            Message msg = new Message("SyncProducer-TopicTest" /* Topic */,
                    "TagA" /* Tag */,
                    ("Hello RocketMQ " +
                            i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */
            );
            //Call send message to deliver message to one of brokers.
            SendResult sendResult = producer.send(msg);
            Thread.sleep(2000);
            System.out.printf("%s%n", sendResult);
        }
        //Shut down once the producer instance is not longer in use.
        Thread.sleep(1000000);
        producer.shutdown();
        /**
         * SendResult [sendStatus=SEND_OK, msgId=C0A801784DE818B4AAC208200DD40060, offsetMsgId=C0A801DD00002A9F000000000001ADDC,
         * messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=3], queueOffset=99]
         */
    }
}
